/**
 * Copyright (c) Chris Myers 2007
 * Aug 12, 2007
 */
package com.chrisandjo.rostra.service;

import com.chrisandjo.rostra.dao.PersonDao;
import com.chrisandjo.rostra.dao.RosterDao;
import com.chrisandjo.rostra.domain.Person;
import com.chrisandjo.rostra.domain.PersonEntity;
import com.chrisandjo.rostra.domain.Roster;
import com.chrisandjo.rostra.domain.RosterEntity;

import java.util.List;
import java.util.Set;

/**
 * @author Chris
 */
public class RosterServiceImpl implements RosterService {
    private PersonDao personDao;
    private RosterDao rosterDao;


    public RosterServiceImpl(RosterDao rosterDao, PersonDao personDao) {
        this.rosterDao = rosterDao;
        this.personDao = personDao;
    }

    public final Roster createNewRoster(String rosterName) {
        final Roster roster = new RosterEntity(rosterName);
        if (rosterDao.rosterExists(rosterName)) {
            throw new IllegalArgumentException("The roster " +
                    rosterName +
                    " already exists");
        }
        rosterDao.save(roster);
        return roster;
    }

    public final void addPersonToRoster(long personId, long rosterId) {
        final Roster roster = rosterDao.getRosterById(rosterId);
        Person person = personDao.getPersonById(personId);
        roster.addPerson(person);
        rosterDao.save(roster);
    }

    public Person createNewPerson(String name) {
        final Person person = new PersonEntity(name);
        personDao.save(person);
        return person;
    }

    public List<Roster> getRosters() {
        return rosterDao.getRosters();
    }

    public Set<Person> getPeople() {
        return personDao.getPeople();
    }

    public Set<Person> getPeopleOnRoster(long rosterId) {
        return rosterDao.getRosterById(rosterId).getPeople();
    }

    public Roster getRoster(long id) {
        return rosterDao.getRosterById(id);
    }

    public Person getPerson(long id) {
        return personDao.getPersonById(id);
    }

}
